package com.gin.no_gay_alliance.activity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.Toolbar;

import com.gin.no_gay_alliance.R;
import com.gin.no_gay_alliance.adpter.ReportingRecordAdapter;
import com.gin.no_gay_alliance.app.MyApplication;
import com.gin.no_gay_alliance.database.dao.ReportingRecordDao;
import com.gin.no_gay_alliance.databinding.ActivityReportingRecordBinding;
import com.gin.no_gay_alliance.utils.AlertDialogUtils;

/**
 * 举报记录活动
 *
 * @author : ginstone
 * @version : v1.0.0
 * @since : 2024/1/24 10:45
 **/
public class ReportingRecordActivity extends BaseBindingActivity<ActivityReportingRecordBinding> {

    private ReportingRecordAdapter adapter;
    private ReportingRecordDao dao;

    /**
     * 解析意图
     *
     * @param intent 意图
     */
    @Override
    protected void handleIntent(Intent intent) {

    }

    /**
     * onCreate中的后续操作
     */
    @Override
    protected void onCreate() {
        adapter = new ReportingRecordAdapter(this);
        b.recyclerView.setAdapter(adapter);
        dao = MyApplication.app.ngaDatabase.reportingRecordDao();
    }

    /**
     * 装载右上角菜单
     *
     * @param menu         菜单
     * @param menuInflater 菜单渲染器
     */
    @Override
    protected void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) {
        menuInflater.inflate(R.menu.reporting_record_menu, menu);
    }

    @Override
    protected Toolbar initToolbar() {
        return b.toolbar;
    }

    @Override
    protected void onResume() {
        super.onResume();

        adapter.updateData(dao.listAll());
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        // 清空记录
        if (item.getItemId() == R.id.menu_item_delete) {
            AlertDialogUtils.showConfirm(this, "清空举报记录", "确认清空?", (dialog, which) -> {
                if (which == DialogInterface.BUTTON_POSITIVE) {
                    dao.deleteAll();
                    handler.post(() -> adapter.clear());
                }
            });

            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public static void startActivity(Context context) {
        Intent intent = new Intent(context, ReportingRecordActivity.class);
        context.startActivity(intent);
    }
}
